import Vue from 'vue'
import Router, { RouteConfig } from 'vue-router'
import Layout from '@/layout/index.vue'
import navRoutes from './nav.config'

Vue.use(Router)

const constantRoutes: RouteConfig[] = [
  {
    path: '/component',
    component: Layout,
    redirect: '/component/quickstart',
    children: [
      // todo test
      // {
      //   path: 'kanban/appoint/appointlist',
      //   component: () => import('@/views/component/nav/index.vue'),
      //   meta: {
      //     secActiveIndex: '/kanban/appoint/',
      //     thirdActiveIndex: '/kanban/appoint/appointlist',
      //   },
      // },
      // {
      //   path: 'kanban/appoint/appointinspect/2/1',
      //   component: () => import('@/views/component/nav/index.vue'),
      //   meta: {
      //     secActiveIndex: '/kanban/appoint/',
      //     thirdActiveIndex: '/kanban/appoint/appointinspect/2/1',
      //   },
      // },
      // todo test end
    ],
  },
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    children: [
      {
        path: '/home',
        component: () => import(/* webpackChunkName: "vender" */ '@/views/home/index.vue'),
        meta: {
          title: '首页',
        },
      },
      {
        path: '/guide',
        component: () =>  import(/* webpackChunkName: "vender" */ '@/views/guide/index.vue'),
        meta: {
          title: '指南',
        },
      },
    ],
  },
  {
    path: '/404',
    component: () => import(/* webpackChunkName: "vender" */ '../views/error-page/404.vue'),
    meta: { hidden: true },
  },
  {
    path: '*',
    redirect: '/404',
  },
]
// 处理 nav 路由【目前写死这里，所有的组件路径 demo 都在 component 文件夹下面，入口 index.vue】
constantRoutes[0].children?.push(...navRoutes)

const createRouter = () => new Router({
  // mode: 'history',  // Disabled due to Github Pages doesn't support this, enable this if you need.
  scrollBehavior: (to, from, savedPosition) => {
    if (savedPosition) {
      return savedPosition
    } else {
      return { x: 0, y: 0 }
    }
  },
  base:'/',
  routes: constantRoutes,
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter();
  (router as any).matcher = (newRouter as any).matcher // reset router
}
(window as any).$router = router
export default router
